﻿using System;

namespace SMS.Gateway.Libs
{
    public class Utils
    {
        // Methods
        private static int Char2Bin(char ch)
        {
            int num = Convert.ToInt32(ch);
            if ((0x39 >= num) && (num >= 0x30))
                return (num - 0x30);

            if ((90 >= num) && (num >= 0x41))
                return ((num - 0x41) + 10);

            if ((0x7a >= num) && (num > 0x61))
                num = (num - 0x61) + 10;

            return num;
        }

        private string A2X(string asc)
        {
            int nLines;
            int nChars;
            int offset;
            string hex = " ";

            // Calc Hexa row(s). 
            if ((asc.Length % 16) > 0)
                nLines = asc.Length / 16 + 1;
            else
                nLines = asc.Length / 16;

            // Convert to Hexa row(s).
            for (int i = 0; i < nLines; i++)
            {
                offset = i * 16;
                if ((asc.Length - offset) > 16)
                    nChars = 16;
                else
                    nChars = asc.Length - offset;
                hex += this.HexLine(i, asc.Substring(offset, nChars)) + "\r\n";
            }
            return hex;
        }

        /// <summary>
        /// Convert string ASCII 16 byte to hexa.
        /// </summary>
        private string HexLine(int lNum, string asc)
        {
            string hex = "";

            // Copy line to buffer.
            char[] c = new char[16];
            asc.CopyTo(0, c, 0, asc.Length);

            // Create offset.
            hex += String.Format("{0:X8} - {0:X8}", lNum * 16, (lNum + 1) * 16 - 1);
            hex += " ";

            // Convert character to hexa.
            for (int i = 0; i < asc.Length; i++)
            {
                if ((i != 0) && ((i % 4) == 0))
                    hex += " ";
                hex += String.Format("{0:X2}", (int)c[i]);
            }

            // Add space.
            int nSpaces = 62 - hex.Length;
            for (int i = 0; i < nSpaces; i++)
                hex += " ";

            // Convert ASCII to ending.                                                                                                  
            for (int i = 0; i < asc.Length; i++)
            {
                if (((int)c[i] < 32) || ((int)c[i] > 126))
                    hex += ".";
                else
                    hex += c[i].ToString();
            }

            // Return hexa .
            return hex;
        }

        public static byte[] Hex2Bin(string hex)
        {
            int length = hex.Length;
            byte[] buffer = new byte[length / 2];
            for (int i = 0; i < length; i += 2)
            {
                int num3 = Char2Bin(hex[i]);
                int num4 = Char2Bin(hex[i + 1]);
                buffer[i / 2] = (byte)(((num3 << 4) & 240) | (num4 & 15));
            }
            return buffer;
        }

        public static string[] SplitMessage(string s)
        {
            int num = s.Length / 0xfe;
            if ((s.Length % 0xfe) > 0)
                num++;

            string[] strArray = new string[num];
            for (int i = 0; i < num; i++)
            {
                if (i != (num - 1))
                    strArray[i] = s.Substring(i * 0xfe, 0xfe);
                else
                    strArray[i] = s.Substring(i * 0xfe, s.Length - (i * 0xfe));
            }
            return strArray;
        }
    }
}

